import { _ as _sliced_to_array } from "@swc/helpers/_/_sliced_to_array";
import { _ as _to_consumable_array } from "@swc/helpers/_/_to_consumable_array";
import { useState } from "react";
import { useDispatch, useSelector } from "react-redux";
import kingsInstance from "../instance";
import { EVENT } from "../enums";
import toast from "../compoents/toast";
import i18n, { LocalKeyMap } from "../local";
// const msgMap:Record<number,string> = {
//   1208: 'An account is already registered with this email address',
//   1011001: 'Message send fail,please try later',
//   1011002: 'Message send fail,please try later',
//   1201: 'No access',
//   1300: 'You do not have enough assets, please check and retry.',
//   1301: 'You do not have enough gas fee, please check and retry.',
//   1302: 'The address you have entered is incorrect, please check and try again.',
//   1214: 'Failed! The account already exists',
// };
var msgMap = {
    1208: i18n.t(LocalKeyMap["interface.error.1208"]),
    1011001: i18n.t(LocalKeyMap["interface.error.1011001"]),
    1011002: i18n.t(LocalKeyMap["interface.error.1011001"]),
    1201: i18n.t(LocalKeyMap["interface.error.1201"]),
    1300: i18n.t(LocalKeyMap["interface.error.1300"]),
    1301: i18n.t(LocalKeyMap["interface.error.1301"]),
    1302: i18n.t(LocalKeyMap["interface.error.1302"]),
    1214: i18n.t(LocalKeyMap["interface.error.1214"])
};
function useRequest(service) {
    var dispatch = useDispatch();
    /** 获取当前路由的meta信息 */ var meta = useSelector(function(rootState) {
        return rootState.app.meta;
    }) || {};
    var _useState = _sliced_to_array(useState(function() {
        return Object.keys(service).reduce(function(pre, next) {
            //@ts-ignore
            pre[next] = function() {
                for(var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++){
                    args[_key] = arguments[_key];
                }
                var _service;
                /** 打开loading */ dispatch.app.SET_LOADING(true);
                return (_service = service)[next].apply(_service, _to_consumable_array(args)).then(function(res) {
                    //@ts-ignore
                    if (res.code == 402 || res.code == 401 || res.code == -2 || res.code == 1201 || res.code == 1210 || res.code == 1202 || res.code == 1211) {
                        setTimeout(function() {
                            /** 白名单路由不用跳转 */ if (!meta.allow) {
                                dispatch.app.PUSH("/login");
                                kingsInstance.emit(EVENT.close);
                            }
                        }, 300);
                    }
                    if (res.code == 502) {
                        res.msg = "The network is unstable, please try again！";
                    }
                    if (res.code !== 200) {
                        res.msg = msgMap[res.code] || res.msg;
                    }
                    return res;
                }).catch(function(err) {
                    var _err_response, _err_response_data;
                    var code = err === null || err === void 0 ? void 0 : (_err_response = err.response) === null || _err_response === void 0 ? void 0 : (_err_response_data = _err_response.data) === null || _err_response_data === void 0 ? void 0 : _err_response_data.code;
                    if (code == 402 || code == 401 || code == -2) {
                        setTimeout(function() {
                            if (!meta.allow) {
                                dispatch.app.PUSH("/login");
                            }
                        }, 300);
                    } else {
                        toast.createToast({
                            message: err === null || err === void 0 ? void 0 : err.message,
                            type: "errToast"
                        });
                    }
                    return Promise.reject(err);
                }).finally(function() {
                    dispatch.app.SET_LOADING(false);
                });
            };
            return pre;
        }, {});
    }), 1), services = _useState[0];
    return services;
}
export default useRequest;

 //# sourceMappingURL=useRequest.js.map